﻿using CubicFWNet.Application.SystemManager;
using CubicFWNet.Data.Models;
using CubicFWNet.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace CubicFWNet.Mvc.Controllers
{
    public class AnnouncementController : ControllerBase
    {
        private AnnouncementApp _newsApp;
        private AnnouncementTypeApp _newstypeApp;

        public AnnouncementController()
        {
            _newsApp = new AnnouncementApp();
            _newstypeApp = new AnnouncementTypeApp();
        }
        // GET: Learning
        public ViewResult Index(int page = 1)
        {
            return View(_newsApp.GetLearningList().ToPagedList(page, 10));
        }

        public ActionResult Add()
        {
            ViewBag.Modules = _newstypeApp.GetLearningList();
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(News news)
        {
            //News news = new News();
            //news.Id = Guid.NewGuid();
            //news.Name = fc["ProductName"];
            //news.Number = fc["Number"];
            //pro.Format = fc["Format"];
            //pro.Memo = fc["Memo"];
            //pro.Data = fc["Data"];
            //pro.Type = Convert.ToInt32(fc["Type"]);
            //pro.Price = Convert.ToDecimal(fc["Price"]);

            news.CreatorID =operatorModel.UserId;
            news.UserList = "";
            news.CreateTime = DateTime.Now;
            int i = _newsApp.AddOrUpdate(news);
            if (i == 1)
            {
                return RedirectToAction("Index", "Announcement");
            }
            else
            {
                ViewBag.Info = "抱歉，添加失败！";
            }

            return View();
        }

        public ActionResult GetFormJson(Guid id)
        {
            var data = _newsApp.GetForm(id);
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public string Delete(Guid id)
        {

            if (_newsApp.Delete(id) == -404)
            {
                Result.Status = "error";
                Result.Message = "未找到该数据";
            }
            return Result.ToJson();
        }
        /// <summary>
        /// 产品编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            string pid = Request.QueryString["PID"];
            Guid id = new Guid(pid);
            News n = _newsApp.GetForm(id);
            if (n != null)
            {
                ViewBag.Modules = _newstypeApp.GetLearningList();
                ViewBag.n = n;
            }
            return View();
        }

        /// <summary>
        /// 产品编辑[提交表单]
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public  ActionResult Edit(News news)
        {
            news.CreatorID = operatorModel.UserId;
            news.UserList = "";
            news.CreateTime = DateTime.Now;
            if (news != null)
            {
                int i = _newsApp.Update(news);
                if (i == 1)
                {
                    //return Result.ToJson();
                    return RedirectToAction("Index", "Announcement");
                }
                else
                {
                    ViewBag.Info = "抱歉，添加失败！";
                }
            }
            return View();
        }

    }
}